package com.GameInterfaceDevelopment.JComboBoxExample;
import javax.swing.*;
import java.awt.*;
public class JComboBoxExample extends JFrame {
    JComboBox cb1,cb2;
    String cityName[]={"山西","上海","北京","重庆","南京","武汉"};
    public JComboBoxExample(){
        super("组合框");
        cb1 = new JComboBox(cityName);
        cb2 = new JComboBox(cityName);
        //设置程序运行时默认被选中的选项
        cb1.setSelectedIndex(0);
        //设置选项不可用
        cb1.setEditable(false);
        //设置所选项目
        cb2.setSelectedItem(cityName[2]);
        cb2.addItem("深圳");
        cb2.setEditable(true);
        Container c = getContentPane();//获取内容面板
        c.setLayout(new FlowLayout());
        c.add(cb1);
        c.add(cb2);
        //设置窗口属性
        setSize(300,300);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        JComboBoxExample jcbe = new JComboBoxExample();
        //在一个JFrame执行关闭操作时，将退出程序。
        jcbe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
